//
//  CodeSeeViewController.m
//  AAIM
//
//  Created by 远的小布丁 on 2020/1/18.
//  Copyright © 2020 AAIM. All rights reserved.
//

#import "CodeSeeViewController.h"
#import "ContactRequestLogic.h"

@interface CodeSeeViewController ()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField * inputTextFiled;

@end

@implementation CodeSeeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     // 导航右按钮
           UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
           [rightBtn setTitle:@"保存" forState:UIControlStateNormal];
           [rightBtn addTarget:self action:@selector(saveClick:) forControlEvents:UIControlEventTouchUpInside];
           [rightBtn sizeToFit];
           [rightBtn setBackgroundColor:KHEXCOLOR(kMainColor)];
           rightBtn.frame = CGRectMake(0, 0, 40, 25);
           rightBtn.layer.cornerRadius = 2;
           rightBtn.layer.masksToBounds = YES;
           rightBtn.titleLabel.font = kMainFont(12);
           rightBtn.jk_touchAreaInsets = UIEdgeInsetsMake(20, 20, 20, 20);
           UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
           self.gk_navigationItem.rightBarButtonItem = rightItem;
           self.gk_navTitle = @"口令看看";
            _model =[LoginManager shareLoginManager].userModel;
            [self.view addSubview:self.inputTextFiled];
               [self setUI];
//               if (_contentStr && ![_contentStr isEqualToString:_model.nickname]) {
              if (_contentStr) {
                  self.inputTextFiled.text = _contentStr;
               }else{
                    self.inputTextFiled.placeholder = @"请输入口令";
               }
}

- (void)setUI{
    [_inputTextFiled mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(15.f));
           make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-15.f));
           make.top.equalTo(self.view.mas_top).offset(kTopHeight);
           make.height.offset(43.f);
       }];
       UIView * line = [UIView new];
       line.backgroundColor = KHEXCOLOR(IM_Color_SeparateLine);
       [self.view addSubview:line];
       [line mas_makeConstraints:^(MASConstraintMaker *make) {
           make.left.equalTo(self.view.mas_left).offset(SCALAE_WIDTH(15.f));
           make.right.equalTo(self.view.mas_right).offset(SCALAE_WIDTH(-15.f));
           make.top.equalTo(self.inputTextFiled.mas_bottom);
           make.height.offset(0.5f);
       }];
}
//保存
- (void)saveClick:(UIButton *)sender{
    [RequestData PostWithToken:_model.token userUUid:_model.uuid withCode:_inputTextFiled.text Success:^(NSDictionary * _Nonnull Dict) {
        NSLog(@"设置口令%@",Dict);
        if ([Dict[@"code"] integerValue] == 200) {
            [self showTipHudWithText:@"验证成功"];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:^(NSString * _Nonnull errMsg) {
        if (errMsg) {
            [self showTipHudWithText:errMsg];
        }else{
         [self showTipHudWithText:@"保存失败"];
        }
    }];
}

-(void)changeNicknameActive:(UITextField *)textFiled{
    NSLog(@"修改");
    
}

//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  // return NO to not change text
//{
//
//    //    //限制其输入长度
//    if (range.length + range.location > textField.text.length) {
//        return NO;
//    }
//    NSUInteger length = textField.text.length + string.length - range.length;
//
//        return length <= 6;
//
//}

- (UITextField *)inputTextFiled{
    if (!_inputTextFiled) {
        _inputTextFiled = [[UITextField alloc]init];
        _inputTextFiled.font = kMainFont(15);
        _inputTextFiled.delegate =self;
        _inputTextFiled.textColor = KHEXCOLOR(@"#262B2F");
        _inputTextFiled.borderStyle = UITextBorderStyleNone;
        [_inputTextFiled addTarget:self action:@selector(changeNicknameActive:) forControlEvents:UIControlEventEditingChanged];
    }
    return _inputTextFiled;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
